package com.aerilys.taskboard.client.activities;

import java.util.List;

import com.aerilys.taskboard.client.NoteService;
import com.aerilys.taskboard.client.NoteServiceAsync;
import com.aerilys.taskboard.client.places.GeneralViewPlace;
import com.aerilys.taskboard.client.tools.ClientFactory;
import com.aerilys.taskboard.client.views.GeneralView;
import com.aerilys.taskboard.shared.Note;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class GeneralActivity extends AbstractActivity implements GeneralView.Presenter
{

  private ClientFactory clientFactory;
  private GeneralViewPlace place;
  
  public static Note ActualNote;
  
  private final NoteServiceAsync greetingService = GWT.create(NoteService.class);

  public GeneralActivity(GeneralViewPlace place, ClientFactory clientFactory)
  {
    this.place = place;
    this.clientFactory = clientFactory;
  }
  
  @Override
  public void start(AcceptsOneWidget panel, EventBus eventBus)
  {
    GeneralView generalView = clientFactory.getGeneralView();
    generalView.setPresenter(this);
    panel.setWidget(generalView.asWidget());
    
    loadDatas();
  }
  
  public void loadDatas()
  {
    greetingService.getNotes(new AsyncCallback<List<Note>>()
        {
          @Override
          public void onFailure(Throwable caught)
          {
            Window.alert("Erreur lors du chargement des taches : "+caught.getMessage());
          }

          @Override
          public void onSuccess(List<Note> result)
          {
            clientFactory.getGeneralView().LoadNotes(result);
          }
         
        });
  }

  @Override
  public void goTo(Place place)
  {
    clientFactory.getPlaceController().goTo(place);
  }

}
